/*
 * barcode.java
 *
 * Created on 21 novembre 2006, 18:38
 *
 * Copyright (C) 2006-2007 Alexis ROBERT
 * Under the terms of the GPL2 license.
 */

package blbarc;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

/**
 * Barcode class
 * @author Alexis ROBERT
 */
public class Barcode {
    String title;
    String barcode;
    int id;
    Map<String,String> fields = new HashMap();
    /** Creates a new instance of barcode */
    public Barcode() {
    }
    
    /** Fill automatically some fields like ISBN */
    public void autoFill() {
        // ISBN
        if (this.barcode.substring(0,3).equals("978")) { // If the barcode begins with 978 ...
            String isbnpart = this.barcode.substring(3,12);
            String finalisbn;
            int avgsum = 0;
            int key;
            
            // Here we calculate the ISBN checksum key
            for (int i = 0; i < isbnpart.length(); i++) {
                avgsum += Integer.parseInt(isbnpart.substring(i,i+1))*(10-i);
            }
            
            key = 11 - (avgsum % 11);
            
            finalisbn = isbnpart.substring(0,1)+"-"+isbnpart.substring(1,5)+"-"+isbnpart.substring(5,9)+"-";
            if (key != 10)
                finalisbn = finalisbn + key;
            else
                finalisbn = finalisbn + "X"; // 0 is X
            
            this.fields.put("ISBN", finalisbn);
        }
    }
}
